package com.jijs.service.client;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.commons.io.FileUtils;
import org.w3c.dom.Document;

import com.sun.xml.ws.api.message.Headers;
import com.sun.xml.ws.developer.WSBindingProvider;

public class ClientTest {
	private IUserService port;
	private UserService us;

	public ClientTest(){
		try {
//			URL url = new URL("http://localhost:9898/us/");
			URL url = new URL("http://localhost:8088/ws_soa/us?wsdl");
			QName name = new QName("http://service.jijs.com/user/", "userService");
			us = new UserService(url,name);
//			us = new UserService();
			port =us.getUserServicePort();
//			Class[] c = port.getClass().getInterfaces();
//			for (Class cc : c) {
//				System.out.println(cc.getName());
//			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
	}
	
	public void testList(){
		List<User> list = port.list();
		for (User user : list) {
			System.out.println(user.getNickname());
		}
	}
	
	public void testAdd() throws ParserConfigurationException{
		
		try{
////			1、将一个对象转换为一个xml通过jaxb
			JAXBContext ctx = JAXBContext.newInstance(LicenceInfo.class);
			User ru = new User();
			ru.setNickname("admin");
			ru.setUsername("admin");
			ru.setPassword("123");
			LicenceInfo info = new LicenceInfo();
			info.setUser(ru);
			QName name = new QName("http://service.jijs.com/user/","licenceInfo");
			JAXBElement<LicenceInfo> jele = new JAXBElement<LicenceInfo>(name, LicenceInfo.class, info);
			Marshaller mars = ctx.createMarshaller();
			mars.setProperty(Marshaller.JAXB_FRAGMENT, true);
			mars.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");;
			
			//2、转换为DOM
			Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
			mars.marshal(jele, doc);
			
			//3、通过Headers.create方法完成header的添加
			//获取WSBindProvider
			WSBindingProvider wsb = (WSBindingProvider)port;
			wsb.setOutboundHeaders(Headers.create(doc.getDocumentElement()));
			
			User u = new User();
			u.setNickname("admin");
			u.setUsername("zhangsan");
			u.setPassword("123");
			port.add(u);
//		} catch (UserException_Exception e) {
//			e.printStackTrace();
//		} catch (JAXBException e) {
//			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void testLogin(){
		try {
			User u = port.login("2323", "sdsd");
			System.out.println(u.getUsername());
		} catch (UserException_Exception e) {
			e.printStackTrace();
		}
	}
	
	public void testDelete(){
		try {
			port.delete(null);
		} catch (UserException_Exception e) {
			e.printStackTrace();
		}
	}
	
	public void testUpload() {  
        try {  
            byte[] file = FileUtils.readFileToByteArray(new File("C:\\Users\\jijs\\Desktop\\msp注册\\register\\Register.jsp"));  
            port.upload(file);  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
	
	public static void main(String[] args) throws ParserConfigurationException {
		ClientTest t = new ClientTest();
//		t.testAdd();
//		t.testList();
//		t.testLogin();
//		t.testDelete();
//		t.testList();
		t.testUpload();
	}
}
